home *** CD-ROM | disk | FTP | other *** search
/ The Atari Compendium / The Atari Compendium (Toad Computers) (1994).iso / files / prgtools / mint / utilit~1 / futilsrc.zoo / fileutil / src / chgrp.c next >
Encoding:
C/C++ Source or Header  |  1991-07-27  |  6.1 KB  |  286 lines

  1. /* chgrp -- change group ownership of files
  2.    Copyright (C) 1989-1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie <djm@ai.mit.edu>. */
  19.  
  20. #include <stdio.h>
  21. #include <ctype.h>
  22. #include <sys/types.h>
  23. #include <grp.h>
  24. #include <getopt.h>
  25. #include "system.h"
  26.  
  27. #ifndef POSIX
  28. struct group *getgrnam ();
  29. void endgrent ();
  30. #endif
  31.  
  32. int lstat ();
  33. int stat ();
  34.  
  35. char *savedir ();
  36. char *xmalloc ();
  37. char *xrealloc ();
  38. int change_file_group ();
  39. int change_dir_group ();
  40. int isnumber ();
  41. void describe_change ();
  42. void error ();
  43. void parse_group ();
  44. void usage ();
  45.  
  46. /* The name the program was run with. */
  47. char *program_name;
  48.  
  49. /* If nonzero, change the ownership of directories recursively. */
  50. int recurse;
  51.  
  52. /* If nonzero, force silence (no error messages). */
  53. int force_silent;
  54.  
  55. /* If nonzero, describe the files we process. */
  56. int verbose;
  57.  
  58. /* If nonzero, describe only owners or groups that change. */
  59. int changes_only;
  60.  
  61. /* A pointer to either lstat or stat. */
  62. int (*xstat) ();
  63.  
  64. /* The name of the group to which ownership of the files is being given. */
  65. char *groupname;
  66.  
  67. struct option long_options[] =
  68. {
  69.   {"dereference", 0, 0, 'L'},
  70.   {"recursive", 0, 0, 'R'},
  71.   {"show-changes", 0, 0, 'c'},
  72.   {"silent", 0, 0, 'f'},
  73.   {"quiet", 0, 0, 'f'},
  74.   {"verbose", 0, 0, 'v'},
  75.   {0, 0, 0, 0}
  76. };
  77.  
  78. void
  79. main (argc, argv)
  80.      int argc;
  81.      char **argv;
  82. {
  83.   int group;
  84.   int errors = 0;
  85.   int optc;
  86.  
  87.   program_name = argv[0];
  88.   recurse = force_silent = verbose = changes_only = 0;
  89.   xstat = lstat;
  90.  
  91.   while ((optc = getopt_long (argc, argv, "LRcfv", long_options, (int *) 0))
  92.      != EOF)
  93.     {
  94.       switch (optc)
  95.     {
  96.     case 'L':
  97.       xstat = stat;
  98.       break;
  99.     case 'R':
  100.       recurse = 1;
  101.       break;
  102.     case 'c':
  103.       verbose = 1;
  104.       changes_only = 1;
  105.       break;
  106.     case 'f':
  107.       force_silent = 1;
  108.       break;
  109.     case 'v':
  110.       verbose = 1;
  111.       break;
  112.     default:
  113.       usage ();
  114.     }
  115.     }
  116.  
  117.   if (optind >= argc - 1)
  118.     usage ();
  119.  
  120.   parse_group (argv[optind++], &group);
  121.  
  122.   for (; optind < argc; ++optind)
  123.     errors |= change_file_group (argv[optind], group);
  124.  
  125.   exit (errors);
  126. }
  127.  
  128. /* Set *G according to NAME. */
  129.  
  130. void
  131. parse_group (name, g)
  132.      char *name;
  133.      int *g;
  134. {
  135.   struct group *grp;
  136.  
  137.   groupname = name;
  138.   if (*name == '\0')
  139.     error (1, 0, "can not change to null group");
  140.  
  141.   grp = getgrnam (name);
  142.   if (grp == NULL)
  143.     {
  144.       if (!isnumber (name))
  145.     error (1, 0, "invalid group `%s'", name);
  146.       *g = atoi (name);
  147.     }
  148.   else
  149.     *g = grp->gr_gid;
  150.   endgrent ();        /* Save a file descriptor. */
  151. }
  152.  
  153. /* Change the ownership of FILE to GID GROUP.
  154.    If it is a directory and -R is given, recurse.
  155.    Return 0 if successful, 1 if errors occurred. */
  156.  
  157. int
  158. change_file_group (file, group)
  159.      char *file;
  160.      int group;
  161. {
  162.   struct stat file_stats;
  163.   int errors = 0;
  164.  
  165.   if ((*xstat) (file, &file_stats))
  166.     {
  167.       if (force_silent == 0)
  168.     error (0, errno, "%s", file);
  169.       return 1;
  170.     }
  171. #ifdef S_ISLNK
  172.   if (S_ISLNK (file_stats.st_mode))
  173.     return 0;
  174. #endif
  175.  
  176.   if (group != file_stats.st_gid)
  177.     {
  178.       if (verbose)
  179.     describe_change (file, 1);
  180.       if (chown (file, file_stats.st_uid, group))
  181.     {
  182.       if (force_silent == 0)
  183.         error (0, errno, "%s", file);
  184.       errors = 1;
  185.     }
  186.     }
  187.   else if (verbose && changes_only == 0)
  188.     describe_change (file, 0);
  189.  
  190.   if (recurse && S_ISDIR (file_stats.st_mode))
  191.     errors |= change_dir_group (file, group, &file_stats);
  192.   return errors;
  193. }
  194.  
  195. /* Recursively change the ownership of the files in directory DIR
  196.    to GID GROUP.
  197.    STATP points to the results of lstat or stat on DIR.
  198.    Return 0 if successful, 1 if errors occurred. */
  199.  
  200. int
  201. change_dir_group (dir, group, statp)
  202.      char *dir;
  203.      int group;
  204.      struct stat *statp;
  205. {
  206.   char *name_space, *namep;
  207.   char *path;            /* Full path of each entry to process. */
  208.   unsigned dirlength;        /* Length of `dir' and '\0'. */
  209.   unsigned filelength;        /* Length of each pathname to process. */
  210.   unsigned pathlength;        /* Bytes allocated for `path'. */
  211.   int errors = 0;
  212.  
  213.   errno = 0;
  214.   name_space = savedir (dir, statp->st_size);
  215.   if (name_space == NULL)
  216.     {
  217.       if (errno)
  218.     {
  219.       if (force_silent == 0)
  220.         error (0, errno, "%s", dir);
  221.       return 1;
  222.     }
  223.       else
  224.     error (1, 0, "virtual memory exhausted");
  225.     }
  226.  
  227.   dirlength = strlen (dir) + 1;    /* + 1 is for the trailing '/'. */
  228.   pathlength = dirlength + 1;
  229.   /* Give `path' a dummy value; it will be reallocated before first use. */
  230.   path = xmalloc (pathlength);
  231.   strcpy (path, dir);
  232.   path[dirlength - 1] = '/';
  233.  
  234.   for (namep = name_space; *namep; namep += filelength - dirlength)
  235.     {
  236.       filelength = dirlength + strlen (namep) + 1;
  237.       if (filelength > pathlength)
  238.     {
  239.       pathlength = filelength * 2;
  240.       path = xrealloc (path, pathlength);
  241.     }
  242.       strcpy (path + dirlength, namep);
  243.       errors |= change_file_group (path, group);
  244.     }
  245.   free (path);
  246.   free (name_space);
  247.   return errors;
  248. }
  249.  
  250. /* Tell the user the group name to which ownership of FILE
  251.    has been given; if CHANGED is zero, FILE was that group already. */
  252.  
  253. void
  254. describe_change (file, changed)
  255.      char *file;
  256.      int changed;
  257. {
  258.   if (changed)
  259.     printf ("group of %s changed to %s\n", file, groupname);
  260.   else
  261.     printf ("group of %s retained as %s\n", file, groupname);
  262. }
  263.  
  264. /* Return nonzero if STR represents an unsigned decimal integer,
  265.    otherwise return 0. */
  266.  
  267. int
  268. isnumber (str)
  269.      char *str;
  270. {
  271.   for (; *str; str++)
  272.     if (!isdigit (*str))
  273.       return 0;
  274.   return 1;
  275. }
  276.  
  277. void
  278. usage ()
  279. {
  280.   fprintf (stderr, "\
  281. Usage: %s [-LRcfv] [+recursive] [+show-changes] [+dereference]\n\
  282.        [+silent] [+quiet] [+verbose] group file...\n",
  283.        program_name);
  284.   exit (1);
  285. }
  286.